package com.jhhc.StormSurgeForecast.AstronomicalTide.Util;



import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author LYQ
 * 时间处理方法
 */
public class TimeUtil {

    private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 获取当前时间
     * @return yyyy-MM-dd HH:mm:ss格式
     */
    public String currentTime(){
        Calendar calendar = Calendar.getInstance();
        return format.format(calendar.getTime());
    }
    public String dateAddYear(String time,int num) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(time));
            calendar.add(Calendar.YEAR,num);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return format.format(calendar.getTime());
    }
    public String averageTime(List<String> timeStrings) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long totalMilliseconds = 0;

        for (String timeString : timeStrings) {
            Date date = dateFormat.parse(timeString);
            totalMilliseconds += date.getTime();
        }

// 计算平均值
        long averageMilliseconds = totalMilliseconds / timeStrings.size();

// 创建平均时间的日期对象
        Date averageDate = new Date(averageMilliseconds);

// 格式化平均时间字符串
        return  dateFormat.format(averageDate);
    }
    /**
     * 获取当前时间的年月日
     * @return TimeObject
     */
    public TimeObject currentTimes(String time) {
        TimeObject timeObject = new TimeObject();
        DecimalFormat format1 = new DecimalFormat("00");
        Calendar calendar = Calendar.getInstance();
        if (time != null && !"".equals(time)) {
            try {
                calendar.setTime(format.parse(time));
            } catch (ParseException e) {
                e.printStackTrace();
                System.out.println("时间转换异常");
            }
        }
        // 获取当前年
        int year = calendar.get(Calendar.YEAR);
        timeObject.setYear(String.valueOf(year));
        // 获取当前月
        int month = calendar.get(Calendar.MONTH) + 1;
        timeObject.setMonth(format1.format(month));
        // 获取当前日
        int day = calendar.get(Calendar.DATE);
        timeObject.setDay(format1.format(day));
        // 获取当前小时
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        timeObject.setHour(format1.format(hour));
        // 获取当前分钟
        int minute = calendar.get(Calendar.MINUTE);
        timeObject.setMinute(format1.format(minute));
        // 获取当前秒
        int second = calendar.get(Calendar.SECOND);
        timeObject.setSecondClock(format1.format(second));
        // 获取当前是本周第几天
        //int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        // 获取当前是本月第几天
        //int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        // 获取当前是本年第几天
        //int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
        return timeObject;
    }
    /**
     * 获取当前时间的年月日
     * @return TimeObject
     */
    public TimeObject currentTimes1(String time) {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        TimeObject timeObject = new TimeObject();
        Calendar calendar = Calendar.getInstance();
        if (time != null && !"".equals(time)) {
            try {
                calendar.setTime(format1.parse(time));
            } catch (ParseException e) {
                e.printStackTrace();
                System.out.println("时间转换异常");
            }
        }
        // 获取当前年
        int year = calendar.get(Calendar.YEAR);
        timeObject.setYear(String.valueOf(year));
        // 获取当前月
        int month = calendar.get(Calendar.MONTH) + 1;
        timeObject.setMonth(String.valueOf(month));
        // 获取当前日
        int day = calendar.get(Calendar.DATE);
        timeObject.setDay(String.valueOf(day));
        // 获取当前小时
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        timeObject.setHour(String.valueOf(hour));
        // 获取当前分钟
        int minute = calendar.get(Calendar.MINUTE);
        timeObject.setMinute(String.valueOf(minute));
        // 获取当前秒
        int second = calendar.get(Calendar.SECOND);
        timeObject.setSecondClock(String.valueOf(second));
        // 获取当前是本周第几天
        //int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        // 获取当前是本月第几天
        //int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        // 获取当前是本年第几天
        //int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
        return timeObject;
    }

    /**
     * 根据传入的年月日时返回时间，格式yyyy-MM-dd HH:mm:ss
     * @param year 年
     * @param month 月
     * @param day 日
     * @param hour 时
     * @return 返回设置的时间
     */
    public String setTime(String year, String month, String day, String hour) {
        DecimalFormat format = new DecimalFormat("00");
        year = format.format(Integer.parseInt(year));
        day = format.format(Integer.parseInt(day));
        hour = format.format(Integer.parseInt(hour));
        return year + "-" + month + "-" + day + " " + hour + ":00:00";
    }
    /**
     * 根据传入的年月日时返回时间，格式yyyy-MM-dd HH:mm:ss
     * @param year 年
     * @param month 月
     * @param day 日
     * @param hour 时
     * @param min 分钟
     * @return 返回设置的时间
     */
    public String setTime(String year, String month, String day, String hour,String min) {
        DecimalFormat format = new DecimalFormat("00");
        year = format.format(Integer.parseInt(year));
        day = format.format(Integer.parseInt(day));
        hour = format.format(Integer.parseInt(hour));
        min = format.format(Integer.parseInt(min));
        return year + "-" + month + "-" + day + " " + hour + ":" + min + ":00";
    }

    /**
     * 判断时间是否是整点数据 yyyy-MM-dd HH:00:00
     * @param time 传入的时间
     */
    public boolean timeEstimate(String time){
        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        int min = 1;
        try {
            calendar.setTime(format.parse(time));
            min = calendar.get(Calendar.MINUTE);
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("时间转换异常");
        }
        return min == 0;
    }
    /**
     * 获取当前时间的年
     * @param time 传入的时间
     * @return time的当前日
     */
    public int currentYear(String time) {
        Calendar calendar = Calendar.getInstance();
        int year = -1;
        try {
            calendar.setTime(format.parse(time));
            year = calendar.get(Calendar.YEAR);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return year;
    }
    /**
     * 获取当前时间的日
     * @param time 传入的时间
     * @return time的当前日
     */
    public int currentDay(String time) {
        Calendar calendar = Calendar.getInstance();
        int day = -1;
        try {
            calendar.setTime(format.parse(time));
            day = calendar.get(Calendar.DAY_OF_MONTH);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return day;
    }
    /**
     * 获取当前时间的小时
     * @param time 传入的时间
     * @return time的当前分钟
     */
    public int currentHour(String time) {
        Calendar calendar = Calendar.getInstance();
        int min = -1;
        try {
            calendar.setTime(format.parse(time));
            min = calendar.get(Calendar.HOUR_OF_DAY);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return min;
    }
    /**
     * 获取当前时间的分钟
     * @param time 传入的时间
     * @return time的当前分钟
     */
    public int currentMin(String time) {
        Calendar calendar = Calendar.getInstance();
        int min = -1;
        try {
            calendar.setTime(format.parse(time));
            min = calendar.get(Calendar.MINUTE);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return min;
    }

    /**
     * 时间加天
     * @param time 传入的时间
     */
    public String dateAddDay(String time,int num) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(time));
            calendar.add(Calendar.DAY_OF_MONTH,num);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return format.format(calendar.getTime());
    }
    /**
     * 时间加小时
     * @param time 传入的时间
     */
    public String dateAddHour(String time,int num) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(time));
            calendar.add(Calendar.HOUR,num);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return format.format(calendar.getTime());
    }

    /**
     * 时间加分钟
     * @param time 传入的时间
     */
    public String dateAddMin(String time,int num) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(time));
            calendar.add(Calendar.MINUTE,num);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return format.format(calendar.getTime());
    }


    /**
     * 时间格式化 "yyyy-MM-dd HH:mm
     * @param time 传入的时间
     */
    public String formatting(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(time));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return format.format(calendar.getTime());
    }

    /**
     * 时间格式化 yyyy/MM/dd HH:mm
     * @param time 传入的时间
     */
    public String timeFormatting1(String time) {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        try {
            Date date = format1.parse(time);
            return format2.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 计算时间差（天）
     * @param startTime 开始
     * @param endTime 结束
     * @return 相差天数
     */
    public int differenceValue(String startTime,String endTime) {
        try {
            return (int) DateUtil.between(format.parse(startTime), format.parse(endTime), DateUnit.DAY);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    /**
     * 比较日期大小
     * @param startTime 开始时间
     * @param endTime 结束时间
     * startTime > endTime -1
     * endTime > startTime 1
     * startTime = endTime 0
     */
    public int compareTime(String startTime,String endTime){
        LocalDateTime start = LocalDateTime.parse(startTime,formatter);
        LocalDateTime end = LocalDateTime.parse(endTime,formatter);
        if (start.isAfter(end)) {
            return -1;
        }else if (start.isBefore(end)) {
            return 1;
        }else {
            return 0;
        }
    }


}
